package simulationEngine;

import java.util.Vector;

import metric.CentralObservableUnit;
import datatypes.Host;

public class Scheduler {

	private SimulationEngine engine = null;
	
	private ApplicationLayerScheduler als = null;
	private TransportLayerScheduler tls = null;
	
	public Scheduler (SimulationEngine engine){
		this.engine = engine;
		als = new ApplicationLayerScheduler(engine);
		tls = new TransportLayerScheduler(engine, als);
	}
	
	public void startStep (int StepID){
		als.init();
		tls.init();
		tls.simulateNext(StepID);
	}
	
	public void invokeStepComplete (int StepID){
		//Write Metric
		int timeStepID = engine.getSimulationEnviroment().getClock().getCurrentTime();
		CentralObservableUnit.simulationStepComplete(engine.getID(),engine.getScenarioParameter().getID(), timeStepID);
		//Network-Simulation is complete, now HostActivitys are simulated
		Vector<Host> tmp = new Vector<Host> (engine.getHostList().getPassiveAvailableHosts().values());
		for (int i = 0; i < tmp.size(); i++){
			Host uniqueHost = tmp.get(i);
			if (uniqueHost != null){
				uniqueHost.getLocalQueue().execute();				
			}
		}
		for (Host uniqueHost : engine.getHostList().getActiveHosts().values()){
			uniqueHost.getLocalQueue().execute();
		}
		//invoke
		engine.getSimulationEnviroment().onStepComplete(StepID);
	}
	
}
